package com.zkingsoft.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.dingtalk.open.client.api.model.corp.MessageBody;
import com.dingtalk.open.client.api.model.corp.MessageType;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Body;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Head;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Body.Form;
import com.zkingsoft.dao.WorkLogDao;
import com.zkingsoft.dingding.Env;
import com.zkingsoft.dingding.Vars;
import com.zkingsoft.dingding.auth.AuthHelper;
import com.zkingsoft.dingding.message.LightAppMessageDelivery;
import com.zkingsoft.dingding.message.MessageHelper;
import com.zkingsoft.model.Bug;
import com.zkingsoft.model.Task;
import com.zkingsoft.model.Users;
import com.zkingsoft.model.WorkLog;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.services.TaskService;
import com.zkingsoft.services.WorkLogService;

public class SendWorkLogThread implements Runnable{
	
	WorkLogService workLogService =null;
	WorkLogDao workLogDao=null;
	WorkLog workLog=null;
	String bgColor = null;
	String title = null;
	String[] str = null;
	Map map = null;
	//发送任务OA消息提醒，OA消息体,map对象中请放入两个key：key,value
	List<Map<String, String>> msgList = null;
	boolean isCreateUser ; //消息发送个谁，true:创建人，false:指派人
	boolean flag = false;
	
	public SendWorkLogThread(WorkLogService workLogService,WorkLog workLog, 
			String bgColor, String title) {
		this.workLogService=workLogService;
		this.workLog = workLog;
		this.bgColor = bgColor;
		this.title = title;
	}
	
	public SendWorkLogThread(WorkLogDao workLogDao,Map map, 
			String bgColor, String title,String[] str) {
		this.workLogDao=workLogDao;
		this.map = map;
		this.bgColor = bgColor;
		this.title = title;
		this.str = str;
	}
	
	@Override
	public void run() {
		if(workLogService!=null){
			flag = sendOaMsgs();
		}
		if(workLogDao!=null){
			flag = sendOaMsgsSerivce();
		}	
	}
	
	
	/**
	 * @Description:批量发送oa通知,供controller层使用
	 * @author: dingchuan
	 * @param taskIds 新增的任务id集合
	 * 返回类型 void
	 * @date 2016年12月5日
	 */
	public boolean sendOaMsgs(){
		try {
			List<Map<String,Object>> uesrs = workLogService.selectUserLog(workLog.getReceiverId());
			if(uesrs!=null && uesrs.size()>0){					
				//循环发送OA消息
				String accessToken = AuthHelper.getAccessToken();//获取accessToken
				String toUsers = "";
				String toParties = Vars.TO_PARTY;
				String agentId = Vars.AGENT_ID;
				for (Map map : uesrs) {					
					//创建oa消息
					MessageBody.OABody oaBody = createOaMsg(workLog);
					// 发送微应用消息
					//给创建人或者指派人发送
					toUsers = (String) map.get("validateCode");
					LightAppMessageDelivery lightAppMessageDelivery = new LightAppMessageDelivery(toUsers, toParties, agentId);
					lightAppMessageDelivery.withMessage(MessageType.OA, oaBody);					
					MessageHelper.send(accessToken, lightAppMessageDelivery);
				}
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;	
	}
	
	/**
	 * @Description:批量发送oa通知,供controller层使用
	 * @author: dingchuan
	 * @param taskIds 新增的任务id集合
	 * 返回类型 void
	 * @date 2016年12月5日
	 */
	public boolean sendOaMsgsSerivce(){
		try {
				//循环发送OA消息
				String accessToken = AuthHelper.getAccessToken();//获取accessToken
				String toUsers = "";
				String toParties = Vars.TO_PARTY;
				String agentId = Vars.AGENT_ID;
					//创建oa消息
					MessageBody.OABody oaBody = createOaMsg1(str);
					// 发送微应用消息
					//给创建人或者指派人发送
					toUsers = (String) map.get("validateCode");
					LightAppMessageDelivery lightAppMessageDelivery = new LightAppMessageDelivery(toUsers, toParties, agentId);
					lightAppMessageDelivery.withMessage(MessageType.OA, oaBody);					
					MessageHelper.send(accessToken, lightAppMessageDelivery);
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;			
	}

	/**
	 * @Description:创建OA消息
	 * chenxianzi
	 * @param task
	 * @return
	 * 返回类型 MessageBody.OABody
	 * @date 2016年12月15日
	 */
	public MessageBody.OABody createOaMsg(WorkLog workLog){
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DD);
		MessageBody.OABody oaBody = new MessageBody.OABody();
        oaBody.setMessage_url(Env.LOOK_WORKLOG +workLog.getWorkLogId() );
        oaBody.setPc_message_url(Env.LOOK_WORKLOG + workLog.getWorkLogId() );
        //头部
        Head head = new Head();
        head.setText("");
        head.setBgcolor(bgColor);
        oaBody.setHead(head);
        //内容
        Body body = new Body();
        body.setTitle(title);
        List<Form> formList = new ArrayList<Form>();
        if(msgList == null){
        	Form form6 = new Form();
        	form6.setKey("服务项目");
        	form6.setValue(workLog.getProjName());
            formList.add(form6);
        	Form form4 = new Form();
            form4.setKey("项目状态");
            form4.setValue(workLog.getStatus());
            formList.add(form4);
            Form form5 = new Form();
            Form form2 = new Form();
            form2.setKey("服务时长");
            form2.setValue(workLog.getWorkingHours());
            formList.add(form2);
            form5.setKey("服务内容");
            form5.setValue(workLog.getWorkContent());
            formList.add(form5);
            Form form = new Form();
            form.setKey("员工姓名");
            form.setValue(workLog.getSenderName());
            formList.add(form);
            Form form3 = new Form();
            form3.setKey("创建时间");
            form3.setValue(sdf.format(workLog.getCreateTime()));
            formList.add(form3);
        }else{
        	for (Map<String, String> map : msgList) {
				Form form = new Form();
				form.setKey(map.get("key"));
				form.setValue(map.get("value"));
				formList.add(form);
			}
        }
        body.setForm(formList);
        oaBody.setBody(body);
		return oaBody;
	}
	/**
	 * @Description:创建OA消息
	 * chenxianzi
	 * @param task
	 * @return
	 * 返回类型 MessageBody.OABody
	 * @date 2016年12月15日
	 */
	public MessageBody.OABody createOaMsg1(String[] dates){
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DD);
		MessageBody.OABody oaBody = new MessageBody.OABody();
        oaBody.setMessage_url(Env.LOOK_WORKLOG1 +map.get("userId") );
        oaBody.setPc_message_url(Env.LOOK_WORKLOG1 +map.get("userId"));
		//头部
		Head head = new Head();
		head.setText("");
		head.setBgcolor(bgColor);
		oaBody.setHead(head);
		//内容
		Body body = new Body();
		body.setTitle(title);
		List<Form> formList = new ArrayList<Form>();
			Form form = new Form();
			form.setKey("日志通知");
			form.setValue(String.format(" 您有%s,%s,%s,3天没有写日志 请及时填写 ",str));
			formList.add(form);
	
		body.setForm(formList);
		oaBody.setBody(body);
		return oaBody;
	}
	
}
